﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using DBService;
using YJKArch.ImportStructureProject.ImportObjects;

namespace YJKArch.ImportStructureProject
{
	internal class ReadStructureInfoFromDbService
	{
		public ReadStructureInfoFromDbService(DbProject project, ref List<ElementId> oldColumnsId, ref List<ElementId> oldBeamsId, ref List<ElementId> oldWallsId, ref List<ElementId> oldFloorsId, ref List<BeamInfos> beamStructureData, ref List<ColumnInfos> columnStructureData, ref List<WallInfos> wallStructureData, ref List<FloorInfos> floorStructureData)
		{
			this.m_Project = project;
			this.m_OldColumnsId = oldColumnsId;
			this.m_OldBeamsId = oldBeamsId;
			this.m_OldWallsId = oldWallsId;
			this.m_OldFloorsId = oldFloorsId;
			this.m_BeamStructureData = beamStructureData;
			this.m_ColumnStructureData = columnStructureData;
			this.m_WallStructureData = wallStructureData;
			this.m_FloorStructureData = floorStructureData;
		}

		public bool ReadStructureInfo(ref string errMessage)
		{
			this.ReadBeamInfo();
			this.ReadColumnInfo();
			this.ReadWallInfo();
			this.ReadFloorInfo();
			this.ReadRemoveInfo();
			return true;
		}

		private void ReadBeamInfo()
		{
			List<DbBeam> list = this.m_Project.BeamToModify();
			this.m_BeamStructureData.Capacity = list.Count + 1;
			foreach (DbBeam dbBeam in list)
			{
				PoleSectionStyle sectionStyle = PoleSectionStyle.CSS_CIRCLE;
				if (dbBeam.SectionParameter.SectionType == DBService.SectionType.Rect)
				{
					sectionStyle = PoleSectionStyle.CSS_RECTANG;
				}
				BeamInfos item = new BeamInfos(new ElementId(dbBeam.RacId), dbBeam.StartPt, dbBeam.EndPt, dbBeam.Bulge, sectionStyle, dbBeam.SectionParameter.Parameter1, dbBeam.SectionParameter.Parameter2);
				this.m_BeamStructureData.Add(item);
			}
		}

		private void ReadColumnInfo()
		{
			List<DbColumn> list = this.m_Project.ColumnToModify();
			this.m_ColumnStructureData.Capacity = list.Count + 1;
			foreach (DbColumn dbColumn in list)
			{
				PoleSectionStyle sectionStyle = PoleSectionStyle.CSS_CIRCLE;
				if (dbColumn.SectionParameter.SectionType == DBService.SectionType.Rect)
				{
					sectionStyle = PoleSectionStyle.CSS_RECTANG;
				}
				ColumnInfos item = new ColumnInfos(new ElementId(dbColumn.RacId), dbColumn.Location, dbColumn.Angle, dbColumn.Height, sectionStyle, dbColumn.SectionParameter.Parameter1, dbColumn.SectionParameter.Parameter2);
				this.m_ColumnStructureData.Add(item);
			}
		}

		private void ReadWallInfo()
		{
			List<DbWall> list = this.m_Project.WallToModify();
			this.m_WallStructureData.Capacity = list.Count + 1;
			foreach (DbWall dbWall in list)
			{
				List<DbOpening> openingList = dbWall.OpeningList;
				List<WallHoleInfos> list2 = new List<WallHoleInfos>();
				foreach (DbOpening dbOpening in openingList)
				{
					if (dbOpening.EdgeLoop.Count > 0)
					{
						WallHoleInfos item = new WallHoleInfos(new ElementId(0), dbOpening.EdgeLoop[0].StartPt, dbOpening.EdgeLoop[0].EndPt);
						list2.Add(item);
					}
				}
				WallInfos item2 = new WallInfos(new ElementId(dbWall.RacId), dbWall.StartPt, dbWall.EndPt, dbWall.Bulge, dbWall.Height, dbWall.Thick, list2);
				this.m_WallStructureData.Add(item2);
			}
		}

		private void ReadFloorInfo()
		{
			List<DbSlab> list = this.m_Project.SlabToModify();
			this.m_FloorStructureData.Capacity = list.Count + 1;
			foreach (DbSlab dbSlab in list)
			{
				List<DbOpening> openingList = dbSlab.OpeningList;
				List<FloorHoleInfos> list2 = new List<FloorHoleInfos>();
				foreach (DbOpening dbOpening in openingList)
				{
					ElementId oldElementId = new ElementId(0);
					List<EdgeInfos> list3 = new List<EdgeInfos>();
					foreach (DbEdge dbEdge in dbOpening.EdgeLoop)
					{
						EdgeInfos item = new EdgeInfos(dbEdge.StartPt, dbEdge.EndPt, dbEdge.Bulge);
						list3.Add(item);
					}
					FloorHoleInfos item2 = new FloorHoleInfos(oldElementId, list3);
					list2.Add(item2);
				}
				DbEdgeLoop edgeLoop = dbSlab.EdgeLoop;
				List<EdgeInfos> list4 = new List<EdgeInfos>();
				foreach (DbEdge dbEdge2 in edgeLoop)
				{
					EdgeInfos item3 = new EdgeInfos(dbEdge2.StartPt, dbEdge2.EndPt, dbEdge2.Bulge);
					list4.Add(item3);
				}
				FloorInfos item4 = new FloorInfos(new ElementId(dbSlab.RacId), list4, list2, dbSlab.Thick);
				this.m_FloorStructureData.Add(item4);
			}
		}

		private void ReadRemoveInfo()
		{
			foreach (DbElementForRemove dbElementForRemove in this.m_Project.ElementForRemove())
			{
				ElementId item = new ElementId(dbElementForRemove.ElementId);
				switch (dbElementForRemove.Category)
				{
				case ElementCategory.Beam:
					this.m_OldBeamsId.Add(item);
					break;
				case ElementCategory.Column:
					this.m_OldColumnsId.Add(item);
					break;
				case ElementCategory.Wall:
					this.m_OldWallsId.Add(item);
					break;
				case ElementCategory.Slab:
					this.m_OldFloorsId.Add(item);
					break;
				}
			}
		}

		private DbProject m_Project;

		private List<ElementId> m_OldColumnsId;

		private List<ElementId> m_OldBeamsId;

		private List<ElementId> m_OldWallsId;

		private List<ElementId> m_OldFloorsId;

		private List<BeamInfos> m_BeamStructureData;

		private List<ColumnInfos> m_ColumnStructureData;

		private List<WallInfos> m_WallStructureData;

		private List<FloorInfos> m_FloorStructureData;
	}
}
